package com.thinkgem.jeesite.modules.customer.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.building.entity.Room;
import com.thinkgem.jeesite.modules.building.service.BuildingService;
import com.thinkgem.jeesite.modules.building.service.RoomService;
import com.thinkgem.jeesite.modules.customer.service.OwnerService;
import com.thinkgem.jeesite.modules.fee.entity.Fee;
import com.thinkgem.jeesite.modules.fee.service.FeeService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value="${adminPath}/owner/tenement")
public class OwnerController extends BaseController{
		@Autowired
		OwnerService ownerService;
		@Autowired
		SystemService systemService;
		@Autowired
		BuildingService buildingService;
		@Autowired
		RoomService roomService;
		@Autowired
		FeeService feeservice;
		@RequestMapping(value = "checkLoginName")
		public String checkLoginName(String oldLoginName, String loginName) {
			if (loginName !=null && loginName.equals(oldLoginName)) {
				return "true";
			} else if (loginName !=null && systemService.getUserByLoginName(loginName) == null) {
				return "true";
			}
			return "false";
		}
		@RequestMapping(value="findAllOwner")
		public String findAllOwner(
				User user,
				HttpServletRequest request,
				HttpServletResponse response,
				Model model){
			user.setIsUser("1");
			Page<User> page = systemService.findUser(new Page<User>(request, response), user);
			model.addAttribute("user", user);
			model.addAttribute("page", page);
			return "modules/customer/ownerList";
		}
		/**
		 * 根据一个userID找到一个对应的用户
		 * */
		
		@RequestMapping(value="findOwnerById")
		public String findOwnerById(@RequestParam String id,Model model){
			model.addAttribute("owner", new User());
			model.addAttribute("owners", systemService.getUser(id));
			return "modules/customer/ownerForm";
		}
		/**
		 * 新增用户表单
		 * */
		@RequestMapping(value="addOwner")
		public String addOwner(HttpServletRequest request,HttpServletResponse response,Model model){
			model.addAttribute("allRoles", systemService.findAllRole());
			model.addAttribute("user", new User());
			model.addAttribute("buildingName", buildingService.findAllBuildingName());
			return "modules/customer/ownerAdd";
		}
		/**
		 * 更新某一个用户的信息
		 * */
		@RequestMapping(value="updateOwner")
		public String updateOwner(User owner,HttpServletRequest request, HttpServletResponse response,Model model){
			systemService.saveUser(owner);
			return "redirect:"+Global.getAdminPath()+"/owner/tenement/findAllOwner";
		}
		
		/**
		 * 用户表单
		 * */
		@RequestMapping(value="ownerForm")
		public String ownerForm(HttpServletRequest request,HttpServletResponse response,Model model,String id){
			model.addAttribute("user", new User());
			model.addAttribute("owners", systemService.getUser(id));
			//model.addAttribute("buildingName", buildingService.findAllBuildingName());
			model.addAttribute("buildingName", buildingService.get(systemService.getUser(id).getRoom().getBuilding().getId()).getBuildingName());
			return "modules/customer/ownerForm";
		}
		/**
		 * 删除某个用户
		 * */
		@RequestMapping(value="deleteOwner")
		public String deleteOwner(@RequestParam("id")String id,Model model){
			Room room = roomService.findRoomByOwnerId(systemService.getUser(id).getId());  //找到这个用户对应的房间
			Fee fee = feeservice.findFeeByRoomIds(room.getId());   //找到这个房间所对应的费率
			/*去除该费率中房间拼接字段，并更新*/
			if(fee!=null){
				String[] roomIds = fee.getRoomIds().split(",");
				String temp = "";
				for(String s:roomIds){
					if(!s.equals(room.getId())) temp+=s+",";
					}
				fee.setRoomIds(temp);
				feeservice.delete(fee);
			}
			systemService.deleteUserAbsolute(id);
			roomService.deleteRoomByUserId(systemService.getUser(id).getId());
			feeservice.deleteUnfeeByUserId(systemService.getUser(id).getId());
			return "redirect:"+Global.getAdminPath()+"/owner/tenement/findAllOwner";
		}
		/**
		 * 跳转到充值界面
		 * */
		@RequestMapping("PayForm")
		public String PayForm(@RequestParam String id,Model model){
			model.addAttribute("owner", systemService.getUser(id));
			return "modules/customer/ownerPay";
		}
		/**
		 * 执行充值命令，更新用户账户
		 * */
		@RequestMapping("Pay")
		public String Pay(User owner,Model model){
			Double money = Double.parseDouble(owner.getBalance());
			User user = systemService.getUser(owner.getId());
			money+=Double.parseDouble(user.getBalance());
			user.setBalance(money+"");
			systemService.saveUser(user);
			return "redirect:"+Global.getAdminPath()+"/owner/tenement/findAllOwner";
		}
		/***
		 * 跳转到用户对应的页面，该页面允许查看和修改
		 * */
		@RequestMapping(value="toUserInfo")
		public String toUserInfo(HttpServletRequest request,HttpServletResponse response,Model model){
			return "redirect:"+Global.getAdminPath()+"/sys/user/info";
		}
		
		/**
		 * 用户页面，查看自己的房产信息
		 * */
		@RequestMapping(value="UserRoomInfo")
		public String UserRoomInfo(HttpServletRequest request,HttpServletResponse response,Model model)
		{
			if(!UserUtils.getUser().getIsUser().equals("1")){
				addMessage(model, "管理员无权进入用户模块");
				return "redirect:"+Global.getAdminPath()+"/sys/user/info";
			}
			Room room = roomService.findRoomByOwnerId(UserUtils.getUser().getId());
			if(room!=null){
				room.setUser(UserUtils.getUser());
				room.setBuilding(buildingService.get(room.getBuilding().getId()));
				model.addAttribute("room", room);
			}
			else{
				model.addAttribute("room", new Room());
			}
			return "modules/customer/ownerRoomInfo";
		}
		/**
		 * 找到自己楼宇的管理员
		 * */
		@RequestMapping(value="findMyAdmin")
		public String findMyAdmin(HttpServletRequest request,HttpServletResponse response,Model model){
			Page<User> page = systemService.findMyAdmin(new Page<User>(request, response),new Page<Room>(request,response),
					new User(),new Room());
			model.addAttribute("page", page);
			return "modules/customer/adminList";
		}
}
